package com.ymlyj666.onlinechat.dao;

import com.ymlyj666.onlinechat.model.FriendShip;
import com.ymlyj666.onlinechat.util.UUID;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class FriendShipDao extends AbstractDao<FriendShip, String> {
    private static final String TABLE_NAME = "tb_friendships";

    public List<FriendShip> findByUidHost(String uidHost) {
        return execute(new MultiQueryAction() {
            @Override
            public String getSQL() {
                return "select * from " + TABLE_NAME + " where uid_host = ?";
            }

            @Override
            public void setParams(PreparedStatement statement) throws SQLException {
                statement.setString(1, uidHost);
            }
        });
    }

    public List<FriendShip> findByUidFriendAndNotAccepted(String uidFriend) {
        return execute(new MultiQueryAction() {
            @Override
            public String getSQL() {
                return "select * from " + TABLE_NAME + " where uid_friend = ? and accepted = false";
            }

            @Override
            public void setParams(PreparedStatement statement) throws SQLException {
                statement.setString(1, uidFriend);
            }
        });
    }

    public FriendShip findByUidPair(String uidHost, String uidFriend) {
        return execute(new SingleQueryAction() {
            @Override
            public String getSQL() {
                return "select * from " + TABLE_NAME + " where uid_host = ? and uid_friend = ?";
            }

            @Override
            public void setParams(PreparedStatement statement) throws SQLException {
                statement.setString(1, uidHost);
                statement.setString(2, uidFriend);
            }
        });
    }

    public int deleteByUidPair(String uidHost, String uidFriend) {
        return (int) execute(new UpdateAction() {
            @Override
            public String getSQL() {
                return "delete from " + TABLE_NAME + " where uid_host = ? and uid_friend = ?";
            }

            @Override
            public void setParams(PreparedStatement statement) throws SQLException {
                statement.setString(1, uidHost);
                statement.setString(2, uidFriend);
            }
        });
    }

    @Override
    public FriendShip save(FriendShip friendShip) {
        execute(new UpdateAction() {
            @Override
            public String getSQL() {
                if (friendShip.getFid() == null) {
                    return "INSERT INTO " + TABLE_NAME + " VALUES (?,?,?,?,?,?)";
                } else {
                    return "UPDATE " + TABLE_NAME + " SET uid_host=?,uid_friend=?,memo_name=?,accepted=? WHERE fid=?";
                }
            }

            @Override
            public void setParams(PreparedStatement statement) throws SQLException {
                if (friendShip.getFid() == null) {
                    String uuid = UUID.randomUUID().toString();
                    friendShip.setFid(uuid);
                    friendShip.setAddTime(System.currentTimeMillis());
                    statement.setString(1, friendShip.getFid());
                    statement.setString(2, friendShip.getUidHost());
                    statement.setString(3, friendShip.getUidFriend());
                    statement.setString(4, friendShip.getMemoName());
                    statement.setLong(5, friendShip.getAddTime());
                    statement.setBoolean(6, friendShip.isAccepted());
                } else {
                    statement.setString(1, friendShip.getUidHost());
                    statement.setString(2, friendShip.getUidFriend());
                    statement.setString(3, friendShip.getMemoName());
                    statement.setBoolean(4, friendShip.isAccepted());
                    statement.setString(5, friendShip.getFid());
                }
            }
        });
        return findOne(friendShip.getFid());
    }

    @Override
    public int delete(String s) {
        return (int) execute(new UpdateAction() {
            @Override
            public String getSQL() {
                return "DELETE FROM " + TABLE_NAME + " WHERE fid=?";
            }

            @Override
            public void setParams(PreparedStatement statement) throws SQLException {
                statement.setString(1, s);
            }
        });
    }

    @Override
    public int delete(FriendShip friendShip) {
        return delete(friendShip.getFid());
    }

    @Override
    public FriendShip findOne(String s) {
        return execute(new SingleQueryAction() {
            @Override
            public String getSQL() {
                return "SELECT * FROM " + TABLE_NAME + " WHERE fid = ?";
            }

            @Override
            public void setParams(PreparedStatement statement) throws SQLException {
                statement.setString(1, s);
            }
        });
    }

    @Override
    public List<FriendShip> findAll() {
        return execute(() -> "SELECT * FROM " + TABLE_NAME);
    }

    @Override
    protected FriendShip transform(ResultSet resultSet) throws SQLException {
        FriendShip friendShip = new FriendShip();
        friendShip.setFid(resultSet.getString("fid"));
        friendShip.setUidHost(resultSet.getString("uid_host"));
        friendShip.setUidFriend(resultSet.getString("uid_friend"));
        friendShip.setAccepted(resultSet.getBoolean("accepted"));
        friendShip.setMemoName(resultSet.getString("memo_name"));
        friendShip.setAddTime(resultSet.getLong("add_time"));
        return friendShip;
    }

}
